﻿#ifndef COMMUNICATION_H
#define COMMUNICATION_H

#include "direction.h"
#include "timeinfo.h"
#include "otherinfo.h"
#include "selfinfo.h"
#include "websocketclient.h"
#include "communicationitem.h"
#include "communicationpage.h"
#include "httpclient.h"
#include "websocketclient.h"
#include "fileoperator.h"
#include "sqliteutil.h"
#include "qsendfiledialog.h"

#include <QWidget>
#include <QMouseEvent>
#include <QApplication>
#include <QScreen>
#include <QQueue>
#include <QUuid>
#include <QDateTime>
#include <QVariantMap>
#include <QFileDialog>
#include <QGraphicsDropShadowEffect>


namespace Ui {
class Communication;
}
#define MARGIN_MIN_SIZE 2
#define MARGIN_MAX_SIZE 6
class Communication : public QWidget
{
    Q_OBJECT

public:
    explicit Communication(HttpClient* httpClient,WebSocketClient* webSocketClient,FileOperator* fileOperator,SQLiteUtil* sQLiteUtil,QString email,QWidget *parent = nullptr);
    ~Communication();
    void insertFirendList(QString email,QString name,QString message);
    bool isMax=false;//用于标识最小化前窗口的状态是否为最大化
    void setMaxStatus();
    void setNormalStatus();
signals:
    void hideComm();
    void receiveFileFinish(QObject* sender,const QString &msgId);
private slots:
    void itemClicked(CommunicationItem* newItem,QWidget* cpage);
    void closeItem(QWidget* item,QWidget* page);
    void downLoadFile(const QString &msgId);
protected:
    bool event(QEvent *event);
    void leaveEvent(QEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent* event);

private:
    Ui::Communication *ui;
    static CommunicationItem* lastItem;
    //工具类引用
    HttpClient* httpClient;
    WebSocketClient* webSocketClient;
    FileOperator* fileOperator;
    SQLiteUtil* sQLiteUtil;
    //窗口引入
    QSendFileDialog *dialog;
    //标识窗口是哪一个账号的
    QString email;
    //处理窗口可移动的问题
//    bool isDragging=false;
//    QPoint lastPos;

    bool m_bIsPressed=false;//是否按下鼠标
    bool m_bIsResizing=false;//是否正在调整窗口，调整窗口大小时不移动窗口
    QPoint m_movePoint;//记录鼠标的位置
    QPoint m_pressPoint;//记录鼠标按住的位置
    Direction m_direction=Direction::NONE;

    void updateRegion(QMouseEvent *event);
    void resizeRegion();
    void extendLeft(QRect &rect);
    void extendRight(QRect &rect);
    void extendTop(QRect &rect);
    void extendDown(QRect &rect);
    void createShadow();


    //设置最大按钮的样式
    void setNormalStyle();
    void setMaximizeStyle();
    //根据用户的账号来查找控件的地址，如果没有返回nullptr
    CommunicationItem* findItem(const QString &email);
    void updateItemInfo(CommunicationItem* item,QVariantMap &map);
    bool isImage(const QString &suffix);
    void addTime(CommunicationPage* page,const qint64 &currentTime);
    void uploadMultiFile(CommunicationItem* item,CommunicationPage* page,QList<QString> &fileList);
    QByteArray toJson(QVariantMap &map);
    QNetworkReply* handleText(QTextFragment &fragment,QVariantMap &map);
    QNetworkReply* handleImage(const QTextImageFormat &imageFormat,QVariantMap &map);
    void setCreateTime(QVariantMap &map);
    void showMessage(CommunicationPage* currentPage,QVariantMap &map,QNetworkReply* reply);
    void saveMessage(const QString &email,QVariantMap &map);

    void loadHttpComponentRespond();
    void loadSocketComponentRespond();
    void loadBaseComponentRespond();

    QString preventDuplicateFilename(const QString &path,QVariantMap &map);
};

#endif // COMMUNICATION_H
